package com.pubg.information.serviceImpl;

import com.alibaba.fastjson.JSONObject;
import com.pubg.information.bean.Rotation;
import com.pubg.information.mapper.CommentMapper;
import com.pubg.information.mapper.RotationMapper;
import com.pubg.information.service.RotationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class RotationServiceImpl implements RotationService {

    @Autowired(required=false)
    RotationMapper rotationMapper;

    @Autowired(required=false)
    CommentMapper commentMapper;

    @Override
    public Map getRotation() {
        HashMap<String,Object> map = new HashMap<>();
        //1轮播图  2公告  3明星新闻 4 流行新闻 5 热点 6选手
        map.put("lbts",rotationMapper.getRotation("1"));
        map.put("notic",rotationMapper.getRotation("2"));
        map.put("stars",rotationMapper.getRotation("3"));
        map.put("pops",rotationMapper.getRotation("4"));
        map.put("hots",rotationMapper.getRotation("5"));
        map.put("players",rotationMapper.getRotation("6"));
        return map;
    }

    @Override
    public List<Rotation> getRotationByType(String type) {
        List<Rotation> list = new ArrayList<Rotation>();
        list = rotationMapper.getRotation(type);
        return list;
    }

    @Override
    public List<Rotation> delectRotationById(String id) {
        List<Rotation> list = new ArrayList<Rotation>();
        String type = rotationMapper.getRotationById(id);
        rotationMapper.updLock(id);
        list = rotationMapper.getRotation(type);
        return list;
    }

    @Override
    public Map insRoation(JSONObject json) {
        Map map = new HashMap();
        Rotation rotation = new Rotation();
        rotation.setLock("1");
        rotation.setContent(json.getString("content"));
        rotation.setSubtitle(json.getString("subtitle"));
        rotation.setTitle(json.getString("title"));
        rotation.setType(json.getString("type"));
        rotation.setImg(json.getString("imgUrl"));
        rotation.setVideo(json.getString("videoUrl"));
        String id = rotationMapper.insRoation(rotation)+"";
        if (!("".equals(id)||null == id)){
            map.put("msg","提交成功");
            List<Rotation> list = rotationMapper.getRotation(json.getString("type"));
            if ("1".equals(json.getString("type"))||"6".equals(json.getString("type"))||"5".equals(json.getString("type"))){
                if (list.size()>5){
                    int[] arr = new int[list.size()];
                    for (int i = 0 ; i < list.size(); i++){
                        arr[i] = list.get(i).getId();
                    }
                    Arrays.sort(arr);
                    String delId = arr[0]+"";
                    rotationMapper.updLock(delId);
                }
            }
            if("2".equals(json.getString("type"))){
                if (list.size()> 1){
                    int[] arr = new int[list.size()];
                    for (int i = 0 ; i < list.size(); i++){
                        arr[i] = list.get(i).getId();
                    }
                    Arrays.sort(arr);
                    String delId = arr[0]+"";
                    rotationMapper.updLock(delId);
                }
            }
            if ("3".equals(json.getString("type"))){
                if (list.size()>2){
                    int[] arr = new int[list.size()];
                    for (int i = 0 ; i < list.size(); i++){
                        arr[i] = list.get(i).getId();
                    }
                    Arrays.sort(arr);
                    String delId = arr[0]+"";
                    rotationMapper.updLock(delId);
                }
            }
            if ("4".equals(json.getString("type"))){
                if (list.size()>4){
                    int[] arr = new int[list.size()];
                    for (int i = 0 ; i < list.size(); i++){
                        arr[i] = list.get(i).getId();
                    }
                    Arrays.sort(arr);
                    String delId = arr[0]+"";
                    rotationMapper.updLock(delId);
                }
            }
        }else{
            map.put("msg","提交失败，请重新提交");
        }
        return map;
    }

    @Override
    public Map selRoation(String id) {
        Map map = new HashMap();
        Rotation rotation = rotationMapper.selRotationById(id);
        List list = commentMapper.selCommentByRotationId(id);
        map.put("rotation",rotation);
        map.put("comList",list);
        return map;
    }
}
